/*
 * DemoHttpState.h
 *
 *  Created on: 2011-11-21
 *      Author: baixp
 */

#ifndef DEMOHTTPSTATE_H_
#define DEMOHTTPSTATE_H_
#include "Lib9.h"
#include "L9Http.h"
using namespace lib9bada;
class DemoHttpState: public Lib9, public L9IState {
private:
	static const int K_Status_None = 0;
	static const int K_Status_Being_Get = 1;
	static const int K_Status_Being_Send = 2;
	int iStatus;
	L9Http* l9http;
	L9Http* pHttp;
	void ShowMessageBox(String msg) {
		showMsgDialog("消息对话框", msg, "确认", 180);
	}
public:
	DemoHttpState(Application* pApp);
	virtual ~DemoHttpState();
	void Init() {
		// TODO Auto-generated method stub
	}

	void Update() {
		// TODO Auto-generated method stub
		if (iStatus == K_Status_None) {
			if (isKeyPressed(K_KEY_1) || getPointerX()>0&&getPointerX() < 200) {
				iStatus = K_Status_Being_Get;
				byte* bin = L9Util::getIntBytes(K_Status_Being_Get);
				//注意联网测试，在android模拟器中使用10.0.2.2代表本机，类似pc上的localhost
				//l9http->openHttp("http://localhost/Lib9/testHttp.aspx", bin);
				l9http->openHttp("http://localhost/lib9/testHttp.aspx", bin,0, 4);
				delete[] bin;
			}
			if (isKeyPressed(K_KEY_3) || getPointerX() > 200) {
				iStatus = K_Status_Being_Send;
				//将数据发送到服务器
				L9OutputStream out;
				out.writeInt(K_Status_Being_Send); //存储标记
				out.writeString("张三");
				out.writeInt(95);
				out.writeString("备注");
				//注意联网测试，在android模拟器中使用10.0.2.2代表本机，类似pc上的localhost
				//                l9http->openHttp("http://localhost/Lib9/testHttp.aspx",
				//                                out.getBytes());
				l9http->openHttp("http://localhost/lib9/testHttp.aspx",out.getBuf(),0,out.getOff());
			}
		} else {
			if (!l9http->isError() && !l9http->isInProcess()) { //http处理结束
				switch (iStatus) {
				case K_Status_Being_Get: {
					pByte bin = l9http->getData();
					L9InputStream in(bin.pChar, 0, bin.size);

					String name = in.readString();
					int score = in.readInt();
					String remark = in.readString();

					String msg=L"来自服务器的消息：\n";
					msg.Append(name);
					msg.Append(",");
					msg.Append(score);
					msg.Append(",");
					msg.Append(remark);

					ShowMessageBox(msg);
				}
					break;
				case K_Status_Being_Send:
					ShowMessageBox("数据发送完毕！");
					break;
				}
				iStatus = K_Status_None;
			} else if (l9http->isError()) {
				ShowMessageBox("联网过程中出现错误");
				iStatus = K_Status_None;
			}
		}
	}

	void Paint() {
		// TODO Auto-generated method stub
		fillScreen(0xFFFFFFFF);
		if (iStatus == K_Status_None) {
			pFG->SetForegroundColor(Color(0xFF000000));
			pL9Str->drawPage(pFG, 0, 4, (SCR_H - 50) >> 1,
					pL9Str->K_Line_Align_Center);
		} else {
			String str = "正在0xFF0000发送数据0x000000中";
			if (iStatus == K_Status_Being_Get) {
				str = "正在0xFF0000接收数据0x000000中";
			}
			pFG->SetForegroundColor(Color(0xFF000000));
			pL9Str->drawLine(pFG, str, 0, SCR_H >> 1, SCR_W,
					pL9Str->K_Line_Align_Center);
		}
	}
};

#endif /* DEMOHTTPSTATE_H_ */
